package com.soft.dao;

import com.soft.IBaseDao.IBaseDao;
import com.soft.pojo.User;
import com.soft.utils.DBUtil;

import java.util.List;

/**
 * @author ruoxi
 * @create 2021-12-24 14:44
 */
public class UserDaoImpl implements IBaseDao<User> {
    @Override
    public int add(User var1) {
        return DBUtil.excute("insert into user values(?,?,?)", null, var1.getUsername(), var1.getPassword());
    }

    @Override
    public int del(int var1) {
        return DBUtil.excute("delete from user where id = ?", var1);
    }


    @Override
    public int update(User var1) {
        return DBUtil.excute("update `user` set username = ?,password = ? where id = ?", var1.getUsername(), var1.getPassword(), var1.getId());
    }

    @Override
    public List<User> selectByAll() {
        return DBUtil.query("select * from user", User.class);
    }

    public List<User> selectUserByPage(int currentPage,int currentCount) {
        return DBUtil.query("select * from user limit ?,?", User.class,currentPage,currentCount);
    }

    /**
     * 登录查询用户名是否存在
     * @param var1 用户输入的用户名和密码保存为User对象
     * @return 返回查到的User对象否则为null
     */
    @Override
    public User selectByKey(User var1) {
        List<User> users = DBUtil.query("select * from user where username = ?", User.class, var1.getUsername());
        if (users.size()>0){
            return users.get(0);
        }
        return null;
    }

    public User selectUserById(int id){
        List<User> users = DBUtil.query("select * from user where id = ?", User.class, id);
        return users.get(0);
    }
}
